home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 April: Mac OS SDK / Dev.CD Apr 99 SDK1.toast / Development Kits / Mac OS USB DDK / Examples / PrinterClassDriver / usbprint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-10  |  7.6 KB  |  332 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        usbprint.c
  3.  
  4.     Contains:    usb printer class device communication
  5.                     (installed in UnitTable)
  6.  
  7.     Version:    xxx put version here xxx
  8.  
  9.  
  10.  
  11.     Copyright: 1998 by Apple Computer, Inc., all rights reserved.
  12.  
  13. */
  14. #include "PrinterClassDriver.h"
  15.  
  16. #ifndef __DEVICES__
  17. #include <devices.h>
  18. #endif
  19.  
  20. #ifndef __FILES__
  21. #include <files.h>
  22. #endif
  23.  
  24. #define kMaskLowByte    0x0FF
  25.  
  26. extern pascal OSErr    DRVRDone( OSErr err, DCtlPtr ctl, IOParamPtr pb );
  27. static void AbortActive( CntrlParam *pb, DCtlPtr clt );
  28.  
  29. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  30.     Name:        GetPrinterStruct
  31.  
  32.     Input Parameters:    IOParamPtr        i/o parameter block
  33.         
  34.     Output Parameters:
  35.         usbPrinterPBStruct    * pointer to the device
  36.         
  37.     Description:
  38.         Given a device control block, map it to a usb device's data structure
  39.  
  40.     Change History:
  41.         28 Feb 1998,    oja:        Original version.
  42. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  43. static struct usbPrinterPBStruct    *
  44. GetPrinterStruct( DCtlPtr ctl )
  45. {
  46.     return (struct usbPrinterPBStruct *) ctl->dCtlStorage;
  47. }
  48.  
  49. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  50.     Name:        Read
  51.  
  52.     Input Parameters:
  53.         
  54.     Output Parameters:
  55.         <none>
  56.         
  57.     Description:
  58.         
  59.     Change History:
  60.         11 Jun 1998,    oja:        return pb->ioResult, not usbprint->in.usbStatus
  61.          4 Apr 1998,    oja:        Fix handshaking: do not set pb->ioResult
  62.         28 Feb 1998,    oja:        Original version.
  63. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  64. static OSErr
  65. Read (IOParamPtr pb, DCtlPtr ctl)
  66. {
  67.     struct usbPrinterPBStruct    *usbprint = GetPrinterStruct( ctl );
  68.  
  69.     //
  70.     //    if we have a unidirectional interface
  71.     //        report a read error
  72.     //        (only status supported by unidirectional is Centronics compatible)
  73.     //
  74.     pb->ioResult = paramErr;    // assume bad
  75.  
  76.     if ( usbprint->printerProtocol == kUSBPrinterUnidirectionalProtocol )
  77.         pb->ioResult = readErr;
  78.         
  79.     else if ( usbprint != NULL )
  80.         (*usbprint->qread)( pb, ctl, usbprint );    //    map the read param block into the usb param block
  81.  
  82.     return pb->ioResult;
  83.  
  84. }
  85.  
  86. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  87.     Name:        Write
  88.  
  89.     Input Parameters:
  90.         
  91.     Output Parameters:
  92.         <none>
  93.         
  94.     Description:
  95.         
  96.     Change History:
  97.         11 Jun 1998,    oja:        return pb->ioResult, not usbprint->out.usbStatus
  98.          4 Apr 1998,    oja:        Fix handshaking: do not set pb->ioResult
  99.         28 Feb 1998,    oja:        Original version.
  100. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  101. static OSErr
  102. Write (IOParamPtr pb, DCtlPtr ctl)
  103. {
  104.     struct usbPrinterPBStruct    *usbprint = GetPrinterStruct( ctl );
  105.  
  106.     pb->ioResult = paramErr;    // assume bad
  107.  
  108.     if ( usbprint != NULL )
  109.         (*usbprint->qwrite)( pb, ctl, usbprint );    //    map the write param block into the usb param block
  110.  
  111.     return pb->ioResult;
  112. }
  113.  
  114. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  115.     Name:        AbortActive
  116.  
  117.     Input Parameters:
  118.         pb
  119.         ctl
  120.         
  121.     Output Parameters:
  122.         <none>
  123.         
  124.     Description:
  125.         
  126.     Change History:
  127.         4 Aug 1998,    oja:        Original version.
  128. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  129. static void
  130. AbortActive( CntrlParam *pb, DCtlPtr ctl )
  131. {
  132.     //        we need to wait here until the transaction is complete
  133.     struct usbPrinterPBStruct    *usbprint = GetPrinterStruct( ctl );
  134.     struct USBPB                    *pActiveUSBIO;
  135.     IOParamPtr                        pActiveIO;
  136.  
  137.     if ( usbprint != NULL )
  138.     {
  139.         if ( pb->ioCRefNum == usbprint->outRefNum )
  140.         {
  141.             pActiveUSBIO = &usbprint->out;
  142.             pActiveIO = usbprint->writeDrvr.pb;
  143.         }
  144.         else
  145.         {
  146.             pActiveUSBIO = &usbprint->in;
  147.             pActiveIO = usbprint->readDrvr.pb;
  148.         }
  149.         if ( pActiveUSBIO->usbCompletion != NULL )
  150.         {
  151.             (*usbprint->qabort)( pb->ioCRefNum, usbprint );    //    cancel outstanding transactions
  152.             //
  153.             //    synchronize data toggle
  154.             // USB addendum, the endpoints of the pipe may be out of sync
  155.             //    a soft reset in the printer class should restore the data toggle
  156.             //
  157.             pb->csCode = kDrvrSoftReset;
  158.             (*usbprint->qstatus)( pb, ctl, usbprint );
  159.  
  160.         }
  161.     }
  162. }
  163.  
  164. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  165.     Name:        DRVROpen
  166.  
  167.     Input Parameters:
  168.         
  169.     Output Parameters:
  170.         <none>
  171.         
  172.     Description:
  173.         
  174.     Change History:
  175.         28 Feb 1998,    oja:        Original version.
  176. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  177.  
  178. pascal OSErr
  179. DRVROpen(CntrlParam *pb, DCtlPtr dce)
  180. {
  181.     return noErr;
  182. }
  183.  
  184. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  185.     Name:        DRVRClose
  186.  
  187.     Input Parameters:
  188.         
  189.     Output Parameters:
  190.         <none>
  191.         
  192.     Description:
  193.         
  194.     Change History:
  195.         28 Feb 1998,    oja:        Original version.
  196. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  197.  
  198. pascal OSErr
  199. DRVRClose(CntrlParam *pb, DCtlPtr ctl)
  200. {
  201.     return noErr;
  202. }
  203.  
  204. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  205.     Name:        DRVRStatus
  206.  
  207.     Input Parameters:
  208.         
  209.     Output Parameters:
  210.         <none>
  211.         
  212.     Description:
  213.         
  214.     Change History:
  215.         30 Jun 1998,    oja:        pass through messages for kDrvrCentronicsStatus,
  216.                                         kDrvr1284IdString,kDrvrSoftReset
  217.         28 Feb 1998,    oja:        Original version.
  218. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  219.  
  220. pascal OSErr
  221. DRVRStatus(CntrlParam *pb, DCtlPtr ctl)
  222. {
  223.     OSErr                err;
  224.     struct             usbPrinterPBStruct    *usbprint = GetPrinterStruct( ctl );
  225.  
  226.     switch ( pb->csCode )
  227.     {
  228.     case kDrvrCentronicsStatus:    //  USB device: centronics status
  229.     case kDrvr1284IdString:     //  USB device: 1284 capability string
  230.     case kDrvrSoftReset:    //  USB device: soft reset
  231.         if ( usbprint != NULL )
  232.             (*usbprint->qstatus)( pb, ctl, usbprint );    //    map the status param block into the usb param block
  233.         err = pb->ioResult;
  234.         break;
  235.     case kDrvrNumDevices:    
  236.         err =  noErr;
  237.         break;
  238.     case 1:    
  239.     case 2:    // deprecated
  240.     default:
  241.         err = paramErr;
  242.         break;
  243.     }
  244.     return err;
  245. }
  246.  
  247. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  248.     Name:        DRVRControl
  249.  
  250.     Input Parameters:
  251.         csCode                        csParam
  252.         ------                        -------
  253.         kDrvrPrivateSetStorage    pointer to the USB device class storage
  254.  
  255.     Output Parameters:
  256.         <none>
  257.         
  258.     Description:
  259.         
  260.     Change History:
  261.         12 Jun 1998,    oja:        default to noErr (was uninitialized with killCode)
  262.         11 Jun 1998,    oja:        abort active usb transactions
  263.          4 Apr 1998,    oja        Removed unused code for private done i/o
  264.                                          pass error code to DrvrDone
  265.         28 Feb 1998,    oja:        Original version.
  266. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  267.  
  268. pascal OSErr
  269. DRVRControl(CntrlParam *pb, DCtlPtr ctl)
  270. {
  271.     OSErr                                err            = noErr;
  272.     struct usbPrinterPBStruct    *usbprint    = GetPrinterStruct( ctl );
  273.  
  274.     switch ( pb->csCode )
  275.     {
  276.     case killCode:
  277.         //
  278.         //    killIO is always handled as an immediate mode transaction
  279.         //
  280.         AbortActive( pb, ctl );
  281.         break;
  282.     case kDrvrPrivateSetStorage:
  283.         //
  284.         //    reference the class driver's private storage
  285.         //        it's not a handle, but devices.h thinks it should be
  286.         //
  287.         ctl->dCtlStorage = (Handle) *((Ptr *) &pb->csParam[0] );
  288.         break;
  289.     default:
  290.         err = paramErr;
  291.         break;
  292.     }
  293.     return err;
  294. }
  295.  
  296. /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  297.     Name:        DRVRPrime
  298.  
  299.     Input Parameters:
  300.         
  301.     Output Parameters:
  302.         <none>
  303.         
  304.     Description:
  305.         
  306.     Change History:
  307.         28 Feb 1998,    oja:        Original version.
  308. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
  309.  
  310. pascal OSErr
  311. DRVRPrime(CntrlParam *pb, DCtlPtr ctl)
  312. {
  313.     OSErr    err = paramErr;
  314.     //
  315.     //    switch on the low order byte to dispatch reads and writes
  316.     //
  317.     if ( (pb->ioTrap & kMaskLowByte) == aRdCmd )
  318.         err = Read( (IOParamPtr) pb, ctl );
  319.     else if ( (pb->ioTrap & kMaskLowByte) == aWrCmd )
  320.         err = Write( (IOParamPtr) pb, ctl );
  321.  
  322.     //
  323.     //    get the ioResult in case the completion routine has already executed
  324.     //    
  325.     if ( err == noErr )
  326.         err = pb->ioResult;
  327.  
  328.     return err;
  329. }
  330.  
  331. // eof
  332.